<?php

namespace app\common;
use net\Http;
use think\facade\Db;

class Convert
{

    public function upload($filename){
        $url = config('web_config.convert_url')."up.php";
        $mime_type = mime_content_type($filename);
        define('MULTIPART_BOUNDARY', '--------------------------' . microtime(true));
        $header = 'Content-Type: multipart/form-data; boundary=' . MULTIPART_BOUNDARY;
        // equivalent to <input type="file" name="uploaded_file"/>
        define('FORM_FIELD', 'img');
        $file_contents = file_get_contents($filename);
        $content = "--" . MULTIPART_BOUNDARY . "\r\n" .
            "Content-disposition: form-data; name=\"" . FORM_FIELD . "\"; filename=\"" . basename($filename) . "\"\r\n" .
            "Content-Type: " . $mime_type . "\r\n\r\n" .
            $file_contents . "\r\n";
        $content .= "--" . MULTIPART_BOUNDARY . "\r\n" .
            "Content-disposition: form-data; name=\"foo\"\r\n\r\n" .
            "bar\r\n";
        $content .= "--" . MULTIPART_BOUNDARY . "--\r\n";
        $context = stream_context_create(array(
            'http' => array(
                'method' => 'POST',
                'header' => $header,
                'content' => $content,
            )
        ));
        $re = file_get_contents($url, false, $context);
        var_dump($re);
        return 3;

    }

    public function main($id){
        $info =$this->getInfo($id);
        $name = $info['filename'];
        $full_name = $info['fileurl'];
        $dir_prev = $info['convert_key'];
        $ext = $info['ext'];
        $filename = root_path().'public/storage'.DIRECTORY_SEPARATOR.$dir_prev.DIRECTORY_SEPARATOR.$full_name;
        $newName = config('web_config.convert_url').$full_name;
        $pdfName = config('web_config.convert_url').$name.'.pdf';
        $jpgName = config('web_config.convert_url').$name.'.jpg';
        if(!is_file($filename)) return 0;
        if(remote_file_exists($newName)){
            $htt = new Http();
            if(remote_file_exists($pdfName) && remote_file_exists($jpgName)){
                $c_path = root_path().'public/storage/wk_files/'.$id;
                $c_newfile  = $c_path.DIRECTORY_SEPARATOR.$full_name;
                $c_pdfname  = $c_path.DIRECTORY_SEPARATOR.$name.'.pdf';
                $c_jpgname  = $c_path.DIRECTORY_SEPARATOR.$name.'.jpg';
                if(!is_file($c_newfile))  $this->move($filename,$c_newfile,$c_path);
                if(!is_file($c_pdfname) && $ext !='pdf' ) $this->download($pdfName,$c_pdfname);
                if(!is_file($c_jpgname))  $this->download($jpgName,$c_jpgname);
                if(is_file($c_newfile) && is_file($c_pdfname) && is_file($c_jpgname)) {
                    $page = $this->getPageNum($c_pdfname);
                    $data = [
                        'imgurl'=>$name.'.jpg',
                        'viewurl'=>$name.'.pdf',
                        'html' =>'storage/wk_files/'.$id.'/',
                        'convert_status'=>2,
                        'page'=>$page
                    ];
                   $re =  Db::name('doc_con')->where('id',$id)->force(true)->save($data);
                   if($re){
                       @unlink($filename);
                       $htt->doGet(config('web_config.convert_url').'de.php?name='.$full_name);
                       return 1;
                   }else{
                       return 0;
                   }
                }
            }else{
                $htt->doGet(config('web_config.convert_url').'co.php?name='.$full_name);
            }
        }else {
            $this->upload($filename);
        }
    }

    public function download($fileurl,$filename){
        file_put_contents($filename,file_get_contents($fileurl));
    }

    public  function getInfo($id){
        $info = Db::name('doc_con')->field('id,fileurl,filename,convert_key,ext')->find($id);
        return $info;
    }

    public function move($oldFlie='',$newFile='',$newPath=''){
        if (!is_dir($newPath)) {
            mkdir($newPath, 0755,true);
        }
        if (is_file($oldFlie)) {
            copy( $oldFlie,$newFile);
        }
        return true;
    }


    public function getPageNum($pdfname){
//        $pdfname = root_path().'result.pdf';
        $pdftext = file_get_contents($pdfname);
        $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
        return $num;
    }

}